package com.jiaai.cloud.wx.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.wx.entity.TWxgoods;
import com.jiaai.cloud.wx.entity.TWxgoodsclass;
import com.jiaai.cloud.wx.service.ITWxgoodsService;
import com.jiaai.cloud.wx.service.ITWxgoodsclassService;
import com.jiaai.cloud.wx.service.ITWxshopclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-09-08
 */
@Controller
@RequestMapping("/api/v1/twxgoodsclass")
public class TWxgoodsclassController {
    private EntityWrapper<TWxgoodsclass> ew = new EntityWrapper<>();
    @Autowired
    private ITWxgoodsclassService itWxgoodsclassService;
    @Autowired
    private ITWxgoodsService itWxgoodsService;

    //todo
    /**
     * 查询商店里的所有商品分类
     * @param shopid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getall/{shopid}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getAll(@PathVariable("shopid") Integer shopid) throws ServiceException {
        ew.setSqlSelect("goodsclassid, goodsclassname");
        ew.eq("shopid", shopid);
        List<TWxgoodsclass> list = itWxgoodsclassService.selectList(ew);
        try {
            return RestResultGenerator.genSuccessResult(list);
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo
    /**
     * 删除商品分类, 该分类的商品的class设置为null
     * @param goodsclassid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/delete/{goodsclassid}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    @Transactional
    public Object delete(@PathVariable("goodsclassid") Integer goodsclassid) throws ServiceException {
        try {
            itWxgoodsService.updateGoodsclass(goodsclassid);
            itWxgoodsclassService.deleteById(goodsclassid);
            return RestResultGenerator.genSuccessResult("删除商品成功");
        }catch (Exception e) {
            throw new ServiceException("删除失败", e.getMessage());
        }
    }

    //todo
    /**
     * 添加一个商品分类
     * @param tWxgoodsclass
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object add(@RequestBody TWxgoodsclass tWxgoodsclass) throws ServiceException {
        try {
            itWxgoodsclassService.insert(tWxgoodsclass);
            return RestResultGenerator.genSuccessResult("添加成功");
        }catch (Exception e) {
            throw new ServiceException("添加失败", e.getMessage());
        }
    }

    //todo
    /**
     * 查询单个商品分类
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getone/{id}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getOne(@PathVariable("id") Integer id) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxgoodsclassService.selectById(id));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo
    /**
     * 编辑商品分类
     * @param tWxgoodsclass
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object update(@RequestBody TWxgoodsclass tWxgoodsclass) throws ServiceException {
        try {
            itWxgoodsclassService.updateById(tWxgoodsclass);
            return RestResultGenerator.genSuccessResult("编辑成功");
        }catch (Exception e) {
            throw new ServiceException("编辑失败", e.getMessage());
        }
    }
}
